package cn.ming.leo.utils;

import java.math.BigDecimal;
import java.math.MathContext;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class FinanceUtil {

	/**
	 * 固定天数计算利润
	 * 
	 * @param amount
	 * @param rate
	 * @param days
	 * @return
	 */
	public static BigDecimal profit(BigDecimal amount, BigDecimal rate, int days) {
		return amount.multiply(rate, MathContext.DECIMAL64).multiply(new BigDecimal(days), MathContext.DECIMAL64)
				.divide(new BigDecimal(36000), 2, BigDecimal.ROUND_HALF_EVEN);
	}

	/**
	 * 固定天数计算利润
	 * 
	 * @param amount
	 * @param rate
	 * @param days
	 * @return
	 */
	public static BigDecimal bourseProfit(BigDecimal amount, BigDecimal rate, int days) {
		return amount.multiply(rate, MathContext.DECIMAL64).multiply(new BigDecimal(days), MathContext.DECIMAL64)
				.divide(new BigDecimal(36500), 2, BigDecimal.ROUND_HALF_EVEN);
	}

	/**
	 * 固定天数计算利润
	 * 
	 * @param amount
	 * @param rate
	 * @param days
	 * @return
	 */
	public static BigDecimal bourseProfit1(BigDecimal amount, BigDecimal rate, int days) {
		return amount.multiply(rate, MathContext.DECIMAL64).multiply(new BigDecimal(days), MathContext.DECIMAL64)
				.divide(new BigDecimal(36500), 4, BigDecimal.ROUND_HALF_EVEN);
	}

	/**
	 * 时间范围内的天数
	 * 
	 * @param smdate
	 * @param bdate
	 * @return
	 */
	public static int daysBetween(Date smdate, Date bdate) {

		Calendar cal = Calendar.getInstance();
		cal.setTime(smdate);
		long time1 = cal.getTimeInMillis();
		cal.setTime(bdate);
		long time2 = cal.getTimeInMillis();
		long between_days = (time2 - time1) / (1000 * 3600 * 24);

		return Math.abs(Integer.parseInt(String.valueOf(between_days)));
	}

	/**
	 * 时间范围内的天数
	 * 
	 * @param smdate
	 * @param bdate
	 * @return
	 * @throws ParseException
	 */
	public static int daysBetween(String smdate, String bdate) throws ParseException {

		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Calendar cal = Calendar.getInstance();
		cal.setTime(sdf.parse(smdate));
		long time1 = cal.getTimeInMillis();
		cal.setTime(sdf.parse(bdate));
		long time2 = cal.getTimeInMillis();
		long between_days = (time2 - time1) / (1000 * 3600 * 24);

		return Math.abs(Integer.parseInt(String.valueOf(between_days)));
	}

	/**
	 * 时间范围内计算利润
	 * 
	 * @param amount
	 * @param rate
	 * @param startDate
	 * @param endDate
	 * @return
	 */
	public static BigDecimal profit(BigDecimal amount, BigDecimal rate, String startDate, String endDate) {
		int days = 0;
		try {
			days = daysBetween(startDate, endDate);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return profit(amount, rate, days);
	}

	/**
	 * 时间范围内计算利润
	 * 
	 * @param amount
	 * @param rate
	 * @param startDate
	 * @param endDate
	 * @return
	 */
	public static BigDecimal bourseProfit(BigDecimal amount, BigDecimal rate, String startDate, String endDate) {
		int days = 0;
		try {
			days = daysBetween(startDate, endDate);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return bourseProfit(amount, rate, days);
	}

	/**
	 * 时间范围内计算利润
	 * 
	 * @param amount
	 * @param rate
	 * @param startDate
	 * @param endDate
	 * @return
	 */
	public static BigDecimal bourseProfit1(BigDecimal amount, BigDecimal rate, String startDate, String endDate) {
		int days = 0;
		try {
			days = daysBetween(startDate, endDate);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return bourseProfit1(amount, rate, days);
	}

	/**
	 * 计算总和
	 * 
	 * @param args
	 * @return
	 */
	public static BigDecimal add(BigDecimal... args) {

		BigDecimal bd = new BigDecimal(0);
		for (BigDecimal arg : args) {
			bd = bd.add(arg);
		}
		return bd;
	}

	public static void main(String[] args) throws ParseException {
		System.out.println(daysBetween("2015-02-02", "2015-02-01"));
	}

}
